package com.qc31.monitorlib.ui.alarms

import android.view.View
import androidx.recyclerview.widget.RecyclerView
import com.github.mikephil.charting.charts.BarChart
import com.github.mikephil.charting.data.BarEntry
import com.qc31.baselib.base.BaseToolbarRecycleActivity
import com.qc31.baselib.utils.ToolbarHelper
import com.qc31.baselib.utils.isFastClick
import com.qc31.baselib.utils.showLongToast
import com.qc31.baselib.utils.startActivityTo
import com.qc31.comlib.Keys
import com.qc31.comlib.entity.HistoryAlarm
import com.qc31.monitorlib.chart.MPChartUtils
import com.qc31.monitorlib.R
import com.qc31.monitorlib.databinding.HeadAdasInfoBinding
import com.qc31.monitorlib.ui.adapter.AlarmHistoryAdapter
import com.qc31.monitorlib.uitls.DataUtil

/** @Author Jasper @Time 2020/12/9 @Desc 主动安全报警列表 */
class AdasInfoActivity : BaseToolbarRecycleActivity() {
    private var carId = ""
    private var title = ""

    private val mAdapter by lazy { AlarmHistoryAdapter() }
    override fun initView() {
        super.initView()
        title = intent.getStringExtra(Keys.INTENT_TITLE) ?: ""
        carId = intent.getStringExtra(Keys.INTENT_CAR_ID) ?: ""
        ToolbarHelper.setToolbar(this, title)
    }

    override fun initRecyclerView() {
        mRecycler.adapter = mAdapter
        mAdapter.addHeaderView(initHeadView())
        mAdapter.setList(DataUtil.alarmEntity?.list)
    }

    override fun initHeadView(): View {
        val binding = HeadAdasInfoBinding.inflate(layoutInflater)

        initChart(DataUtil.alarmEntity, binding.mBarChart)
        return binding.root
    }

    private fun initChart(entity: HistoryAlarm?, mBarChart: BarChart) {
        if (entity == null) return
        val entries = ArrayList<BarEntry>()
        val xLabe = ArrayList<String>()
        for ((index, infoEntity) in entity.list.withIndex()) {
            entries.add(BarEntry(index.toFloat(), infoEntity.total.toFloat()))
            xLabe.add("${index + 1}")
        }
        MPChartUtils.configBarChart(mBarChart, xLabe)
        MPChartUtils.initBarChart(mBarChart, entries, "", barWidth = 0.7f)
    }

    override fun setListener() {
        mAdapter.setOnItemClickListener { adapter, view, position ->
            if (isFastClick()) return@setOnItemClickListener
            val info =
                adapter.data[position] as HistoryAlarm.TotalAlarms
            if (info.total == 0) {
                showLongToast(R.string.toast_no_about_alarm)
            } else {
                startActivityTo<AlarmDetailActivity>(this) {
                    putExtra(Keys.INTENT_CAR_ID, carId)
                    putExtra(Keys.INTENT_PLATE, title)
                    putExtra(Keys.INTENT_TITLE, "${info.name}(${info.total})")
                    DataUtil.alarmInfo = info.list
                }
            }
        }

        binding.tvToTop.setOnClickListener {
            mRecycler.smoothScrollToPosition(0)
        }

        mRecycler.addOnScrollListener(object : RecyclerView.OnScrollListener() {
            override fun onScrolled(recyclerView: RecyclerView, dx: Int, dy: Int) {
                super.onScrolled(recyclerView, dx, dy)
                val position = mLayoutManger.findFirstVisibleItemPosition()
                if (position > 0) {
                    binding.tvToTop.visibility = View.VISIBLE
                } else {
                    binding.tvToTop.visibility = View.GONE
                }
            }
        })
    }
}